From 828247ac1e51e4d5908a0581fe02d527b36ee105 Mon Sep 17 00:00:00 2001 From: tee-too Date: Thu, 13 Jul 2017 13:24:28 +0200 Subject: [PATCH] Fix support of `[target.'cfg(...)']` syntax for rustc and rustdoc flags Support of `[target.'cfg(...)']` for rustc and rustdoc flags is buggy. This adds meaningful tests and fixes the issue. Fixes #3499 --- src/cargo/ops/cargo_rustc/context.rs | 17 +++- tests/rustflags.rs | 135 ++++++++++++++++++--------- 2 files changed, 104 insertions(+), 48 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index f1b113e94..53358ef8c 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -1049,11 +1049,18 @@ fn env_args(config: &Config, // ...including target.'cfg(...)'.rustflags if let Some(ref target_cfg) = target_info.cfg { if let Some(table) = config.get_table("target")? { - let cfgs = table.val.iter().map(|(t, _)| (CfgExpr::from_str(t), t)) - .filter_map(|(c, n)| c.map(|c| (c, n)).ok()) - .filter(|&(ref c, _)| c.matches(target_cfg)); - for (_, n) in cfgs { - let key = format!("target.'{}'.{}", n, name); + let cfgs = table.val.keys().filter_map(|t| { + if t.starts_with("cfg(") && t.ends_with(")") { + let cfg = &t[4..t.len() - 1]; + CfgExpr::from_str(cfg) + .ok() + .and_then(|c| if c.matches(target_cfg) { Some(t) } else { None }) + } else { + None + } + }); + for n in cfgs { + let key = format!("target.{}.{}", n, name); if let Some(args) = config.get_list_or_split_string(&key)? { let args = args.val.into_iter(); rustflags.extend(args); diff --git a/tests/rustflags.rs b/tests/rustflags.rs index 8cdd31f1b..9b319b478 100644 --- a/tests/rustflags.rs +++ b/tests/rustflags.rs @@ -958,33 +958,55 @@ fn cfg_rustflags_normal_source() { name = "foo" version = "0.0.1" "#) - .file("src/lib.rs", "") + .file("src/lib.rs", "pub fn t() {}") .file("src/bin/a.rs", "fn main() {}") .file("examples/b.rs", "fn main() {}") .file("tests/c.rs", "#[test] fn f() { }") - .file("benches/d.rs", r#" - #![feature(test)] - extern crate test; - #[bench] fn run1(_ben: &mut test::Bencher) { }"#) - .file(".cargo/config", " - [target.'cfg(feature=\"feat\")'] - rustflags = [\"-Z\", \"bogus\"] - "); + .file(".cargo/config", &format!(r#" + [target.'cfg({})'] + rustflags = ["--cfg", "bar"] + "#, if rustc_host().contains("-windows-") {"windows"} else {"not(windows)"})); p.build(); - - assert_that(p.cargo("build").arg("--features").arg("\"feat\"") - .arg("--lib"), - execs().with_status(101)); - assert_that(p.cargo("build").arg("--features").arg("\"feat\"") - .arg("--bin=a"), - execs().with_status(101)); - assert_that(p.cargo("build").arg("--features").arg("\"feat\"") - .arg("--example=b"), - execs().with_status(101)); - assert_that(p.cargo("test").arg("--features").arg("\"feat\""), - execs().with_status(101)); - assert_that(p.cargo("bench").arg("--features").arg("\"feat\""), - execs().with_status(101)); + + assert_that(p.cargo("build").arg("--lib").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +")); + + assert_that(p.cargo("build").arg("--bin=a").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +")); + + assert_that(p.cargo("build").arg("--example=b").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +")); + + assert_that(p.cargo("test").arg("--no-run").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[RUNNING] `rustc [..] --cfg bar[..]` +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +")); + + assert_that(p.cargo("bench").arg("--no-run").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[RUNNING] `rustc [..] --cfg bar[..]` +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] release [optimized] target(s) in [..] +")); + } // target.'cfg(...)'.rustflags takes precedence over build.rustflags @@ -996,32 +1018,59 @@ fn cfg_rustflags_precedence() { name = "foo" version = "0.0.1" "#) - .file("src/lib.rs", "") - .file(".cargo/config", " + .file("src/lib.rs", "pub fn t() {}") + .file("src/bin/a.rs", "fn main() {}") + .file("examples/b.rs", "fn main() {}") + .file("tests/c.rs", "#[test] fn f() { }") + .file(".cargo/config", &format!(r#" [build] - rustflags = [\"--cfg\", \"foo\"] + rustflags = ["--cfg", "foo"] - [target.'cfg(feature = \"feat\"')] - rustflags = [\"-Z\", \"bogus\"] - "); + [target.'cfg({})'] + rustflags = ["--cfg", "bar"] + "#, if rustc_host().contains("-windows-") { "windows" } else { "not(windows)" })); p.build(); - assert_that(p.cargo("build").arg("--features").arg("\"feat\"") - .arg("--lib"), - execs().with_status(101)); - assert_that(p.cargo("build").arg("--features").arg("\"feat\"") - .arg("--bin=a"), - execs().with_status(101)); - assert_that(p.cargo("build").arg("--features").arg("\"feat\"") - .arg("--example=b"), - execs().with_status(101)); - assert_that(p.cargo("test").arg("--features").arg("\"feat\""), - execs().with_status(101)); - assert_that(p.cargo("bench").arg("--features").arg("\"feat\""), - execs().with_status(101)); -} + assert_that(p.cargo("build").arg("--lib").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +")); + + assert_that(p.cargo("build").arg("--bin=a").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +")); + + assert_that(p.cargo("build").arg("--example=b").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +")); + assert_that(p.cargo("test").arg("--no-run").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[RUNNING] `rustc [..] --cfg bar[..]` +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +")); + assert_that(p.cargo("bench").arg("--no-run").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg bar[..]` +[RUNNING] `rustc [..] --cfg bar[..]` +[RUNNING] `rustc [..] --cfg bar[..]` +[FINISHED] release [optimized] target(s) in [..] +")); + +} #[test] fn target_rustflags_string_and_array_form1() { -- 2.30.2